const { coupon } = require("../../api.js");

var t = require("../../api.js"), o = getApp();

Page({
    data: {
        coupon_cat: 0,
        pitch: 0
    },
    onLoad: function(n) {
        o.pageOnLoad(this, n);
        this.getList();
        
    },
    getList: function () {
        var e = this;
        var shop_id = wx.getStorageSync('address_info');
        wx.showLoading({
            mask: !0
        }), o.request({
            url: t.default.coupon_list,
            data:{
                shop_id: shop_id.shop_id,
                coupon_cat: e.data.coupon_cat
            },
            success: function(t) {
                0 == t.code && e.setData({
                    coupon_list: t.data.list,
                    ms_list: t.data.mslist,
                    tj_list: t.data.tjlist,
                    cat_list: t.data.catlist
                });
            },
            complete: function() {
                wx.hideLoading();
                e.setTime()
            }
        });
    },
    onReady: function() {},
    onShow: function() {},
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {},
    onReachBottom: function() {},
    receive: function(n) {console.log(n);
        if (!wx.getStorageSync('user_info') || wx.getStorageSync('user_info').length == 0 || !wx.getStorageSync('user_info').id) {
          this.setData({
            user_info_show: !0
          })
          return false;
        }
        var text = n.target.dataset.id > 0 ? n.target.dataset : n.currentTarget.dataset;

        var e = this, i = text.index, key = text.key, type = text.type;
        wx.showLoading({
            mask: !0
        }), o.request({
            url: t.coupon.receive,
            data: {
                id: i
            },
            success: function(t) {
                wx.hideLoading();
                if (0 == t.code) {
                    if (type == 'tj') {
                        var coupon_list = e.data.tj_list;
                        coupon_list[key].is_receive = 1;
                        e.setData({
                            tj_list: coupon_list
                        })
                    }else if (type == 'ms') {
                        var coupon_list = e.data.ms_list;
                        coupon_list[key].is_receive = 1;
                        e.setData({
                            ms_list: coupon_list
                        })
                    }else {
                        var coupon_list = e.data.coupon_list;
                        coupon_list[key].is_receive = 1;
                        e.setData({
                            coupon_list: coupon_list
                        })
                    }
                    e.getList()
                }else {
                    wx.showToast({
                        title: t.msg,
                        duration: 2e3
                    })
                }
            },
            complete: function() {
                
            }
        });
    },
    closeCouponBox: function(t) {
        this.setData({
            get_coupon_list: ""
        });
    },
    goodsList: function(t) {
        var o = t.currentTarget.dataset.goods, cat = t.currentTarget.dataset.cat, n = [],cat_arr = [];
        cat = cat ? JSON.parse(cat) : []
        for (var e in o) n.push(o[e].id);
        for (var i in cat) cat_arr.push(cat[i]);
        wx.navigateTo({
            url: "/pages/list/list?goods_id=" + n +"&cat_id=" + cat_arr,
            success: function(t) {},
            fail: function(t) {}
        });
    },
    pitch(e) {
        let i = e.currentTarget.dataset.index
        let that = this;
        this.setData({
          pitch: i,
          coupon_cat: that.data.cat_list[i].id
        },function() {
            that.getList();
        })
    },
    setTime() {
        var that =  this;
  
        setInterval(function () {
          var date = new Date();
          var now = date.getTime() / 1000;
          now = Math.round(now);
          var ms_list = that.data.ms_list;
          if (ms_list) {

            for(var i =0 ; i< ms_list.length ; i++) {
                var let_time = ms_list[i].ms_end_time - now;
                var start_time = ms_list[i].ms_start_time - now;
                if (start_time > 0) {
                    var d = Math.floor(start_time / 60 / 60 / 24);
                    var h = Math.floor(start_time / 60 / 60 % 24);
                    var m = Math.floor(start_time / 60 % 60);
                    var s = Math.floor(start_time % 60);
                    ms_list[i].s = s < 10 ? "0" + s : s;
                    ms_list[i].i = m < 10 ? "0" + m : m;
                    ms_list[i].h = h < 10 ? "0" + h : h;
                    ms_list[i].d = d < 10 ? "0" + d : d;
                    ms_list[i].time_type = 2; //还没开始
                } else if (let_time >= 0) {
                    var d = Math.floor(let_time / 60 / 60 / 24);
                    var h = Math.floor(let_time / 60 / 60 % 24);
                    var m = Math.floor(let_time / 60 % 60);
                    var s = Math.floor(let_time % 60);
                    ms_list[i].s = s < 10 ? "0" + s : s;
                    ms_list[i].i = m < 10 ? "0" + m : m;
                    ms_list[i].h = h < 10 ? "0" + h : h;
                    ms_list[i].d = d < 10 ? "0" + d : d;
                    ms_list[i].time_type = 1; // 还没结束
                } else {
                    var neax_time = ms_list[i].ms_neax_time - now;
                    if (neax_time > 0) {
                        var d = Math.floor(neax_time / 60 / 60 / 24);
                        var h = Math.floor(neax_time / 60 / 60 % 24);
                        var m = Math.floor(neax_time / 60 % 60);
                        var s = Math.floor(neax_time % 60);
                        ms_list[i].s = s < 10 ? "0" + s : s;
                        ms_list[i].i = m < 10 ? "0" + m : m;
                        ms_list[i].h = h < 10 ? "0" + h : h;
                        ms_list[i].d = d < 10 ? "0" + d : d;
                        ms_list[i].time_type = 2; // 已经结束
                    }else {
                        ms_list[i].s = '00';
                        ms_list[i].i = '00';
                        ms_list[i].h = '00';
                        ms_list[i].d = '00';
                        ms_list[i].time_type = 0; // 已经结束
                    }
                }
            };
          }
          that.setData({
            ms_list: ms_list,
          })
  
  
        }, 1000)
        
        
    },
});